package com.huixuebao.after.util;

import lombok.Getter;
import lombok.Setter;

import java.util.LinkedHashSet;
import java.util.Set;

@Getter
@Setter
public class PasswordChecker {
    private static boolean upperCase = false; // 包含大写字母
    private static boolean lowerCase = false; // 包含小写字母
    private static boolean letter = true; // 包含字母
    private static boolean digit = true; // 包含数字
    private static boolean special = false; // 包含特殊字符
    private static Set<Character> specialCharSet = null; // 特殊字符集合
    private static int minLength = 6; // 最小长度
    private static int maxLength = 16; // 最大长度

    /**
     * 密码符合规则，返回true
     */
    public static boolean check(String password){
        if(specialCharSet == null){
            specialCharSet = defaultSpecialCharSet();
        }

        if(password==null || password.length()<minLength || password.length()>maxLength){
            // 长度不符合
            return false;
        }

        boolean containUpperCase = false;
        boolean containLowerCase = false;
        boolean containLetter = false;
        boolean containDigit = false;
        boolean containSpecial = false;

        for(char ch : password.toCharArray()){
            if(Character.isUpperCase(ch)){
                containUpperCase = true;
                containLetter = true;
            }else if(Character.isLowerCase(ch)){
                containLowerCase = true;
                containLetter = true;
            }else if(Character.isDigit(ch)){
                containDigit = true;
            }else if(specialCharSet.contains(ch)){
                containSpecial = true;
            }else{
                // 非法字符
                return false;
            }
        }

        if(upperCase && !containUpperCase){
            return false;
        }
        if(lowerCase && !containLowerCase){
            return false;
        }
        if(letter && !containLetter){
            return false;
        }
        if(digit && !containDigit){
            return false;
        }
        return !special || containSpecial;
    }

    public static Set<Character> defaultSpecialCharSet(){
        Set<Character> specialChars = new LinkedHashSet<>();
        // 键盘上能找到的符号
        specialChars.add('~');
        specialChars.add('`');
        specialChars.add('!');
        specialChars.add('@');
        specialChars.add('#');
        specialChars.add('$');
        specialChars.add('%');
        specialChars.add('^');
        specialChars.add('&');
        specialChars.add('*');
        specialChars.add('(');
        specialChars.add(')');
        specialChars.add('-');
        specialChars.add('_');
        specialChars.add('+');
        specialChars.add('=');
        specialChars.add('{');
        specialChars.add('[');
        specialChars.add('}');
        specialChars.add(']');
        specialChars.add('|');
        specialChars.add('\\');
        specialChars.add(':');
        specialChars.add(';');
        specialChars.add('"');
        specialChars.add('\'');
        specialChars.add('<');
        specialChars.add(',');
        specialChars.add('>');
        specialChars.add('.');
        specialChars.add('?');
        specialChars.add('/');
        return specialChars;
    }
}
